package com.aysf.drafter.controller;

import com.aysf.drafter.entity.BedLayout;
import com.aysf.drafter.entity.CustomerCheckIn;
import com.aysf.drafter.entity.Result;
import com.aysf.drafter.service.IBedLayoutService;
import com.aysf.drafter.service.ICustomerCheckInService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author MaoZz
 * @since 2024-12-21 17:18:45
 */
@RestController
@CrossOrigin
@RequestMapping("/customerCheckIn")
public class CustomerCheckInController {

    @Autowired
    private IBedLayoutService bedLayoutService;
    @Autowired
    private ICustomerCheckInService customerCheckInService;


    @RequestMapping("/save")
    public Result Save(@RequestBody CustomerCheckIn customerCheckIn) {
        System.out.println(customerCheckIn);
        UpdateWrapper<BedLayout> updateWrapper = new UpdateWrapper<BedLayout>()
                .eq("room_number", customerCheckIn.getRoomNumber())
                .set("bed_status", "占用");
        bedLayoutService.update(updateWrapper);
        QueryWrapper<BedLayout> roomNumber = new QueryWrapper<BedLayout>()
                .eq("room_number", customerCheckIn.getRoomNumber());
        BedLayout one = bedLayoutService.getOne(roomNumber);
        customerCheckIn.setBuilding(one.getBuilding());
        return customerCheckInService.saves(customerCheckIn);
    }


    @RequestMapping("/getall")
    public Result getAll(Integer current, Integer size, String key) {
        Page<CustomerCheckIn> page = new Page<>(current, size);
        Wrapper<CustomerCheckIn> Wrapper =
                new QueryWrapper<CustomerCheckIn>().like("name", key).orderByDesc("customer_id");
        Page<CustomerCheckIn> page1 = customerCheckInService.page(page, Wrapper);
        if (page1.getRecords().size() > 0) {
            return new Result("success", page1);
        } else return new Result("error");
    }


    @RequestMapping("/getone")
    public Result getone(String idCard) {
        CustomerCheckIn checkIn = customerCheckInService.getOne(new QueryWrapper<CustomerCheckIn>()
                .eq("id_card", idCard));
        System.out.println(idCard);
        if (Objects.nonNull(checkIn)) {
            return new Result("success", checkIn);
        } else return new Result("error");
    }


    @RequestMapping("/update1")
    public Result update1(@RequestBody CustomerCheckIn customerCheckIn) {
        boolean update = customerCheckInService.update(
                new UpdateWrapper<CustomerCheckIn>()
                        .set("age", customerCheckIn.getAge())
                        .set("check_in_date", customerCheckIn.getCheckInDate())
                        .set("family_member", customerCheckIn.getFamilyMember())
                        .set("companion", customerCheckIn.getCompanion())
                        .set("evaluation_doctor", customerCheckIn.getEvaluationDoctor())
                        .set("responsible_doctor", customerCheckIn.getResponsibleDoctor())
                        .set("nursing_staff", customerCheckIn.getNursingStaff())
                        .set("phone",customerCheckIn.getPhone())
                        .set("health_manager", customerCheckIn.getHealthManager())
                        .set("remarks",customerCheckIn.getRemarks())
                        .eq("id_card", customerCheckIn.getIdCard()));
        if (update) {
            return new Result("success");
        } else return new Result("error");
    }


}
